```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java异步异常处理指南 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            color: #1a1a1a;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4361ee 0%, #3a0ca3 100%);
        }
        .code-block {
            background: #2d2d2d;
            color: #f8f8f2;
            border-radius: 8px;
            overflow: hidden;
        }
        .code-header {
            background: #1e1e1e;
            padding: 0.5rem 1rem;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .code-body {
            padding: 1rem;
            overflow-x: auto;
        }
        .hover-scale {
            transition: transform 0.3s ease;
        }
        .hover-scale:hover {
            transform: translateY(-5px);
        }
        .method-card {
            border-left: 4px solid #4361ee;
            transition: all 0.3s ease;
        }
        .method-card:hover {
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6">Java异步编程中的异常处理艺术</h1>
                    <p class="text-xl md:text-2xl opacity-90 mb-8">掌握现代Java异步编程中的异常处理技巧，构建健壮的并发应用</p>
                    <div class="flex space-x-4">
                        <a href="#future" class="bg-white text-indigo-700 px-6 py-3 rounded-lg font-medium hover:bg-gray-100 transition duration-300">Future模式</a>
                        <a href="#completable-future" class="bg-transparent border-2 border-white text-white px-6 py-3 rounded-lg font-medium hover:bg-white hover:text-indigo-700 transition duration-300">CompletableFuture</a>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="bg-white bg-opacity-10 p-8 rounded-xl backdrop-blur-sm">
                        <div class="mermaid">
                            graph TD
                                A[同步异常处理] -->|直接抛出| B[调用栈]
                                C[异步异常处理] --> D[Future.get()]
                                C --> E[CompletableFuture]
                                C --> F[响应式流]
                                D --> G[ExecutionException]
                                E --> H[handle/exceptionally]
                                F --> I[onError操作符]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-6xl px-4 py-16">
        <!-- Introduction -->
        <section class="mb-20">
            <div class="bg-white rounded-xl shadow-md p-8 mb-8">
                <h2 class="text-3xl font-bold mb-6 text-gray-800">异步异常处理的挑战</h2>
                <p class="text-lg text-gray-700 mb-6">在Java中处理异步操作中的异常是一个重要的任务，因为异步编程模型通常意味着异常不会像同步代码那样直接被抛出到调用栈中。处理异步操作中的异常通常涉及以下几个方面：</p>
                
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-indigo-50 p-6 rounded-lg hover-scale">
                        <div class="text-indigo-600 mb-4">
                            <i class="fas fa-code-branch text-3xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3">线程边界问题</h3>
                        <p class="text-gray-700">异常发生在不同线程，无法通过传统try-catch捕获</p>
                    </div>
                    <div class="bg-yellow-50 p-6 rounded-lg hover-scale">
                        <div class="text-yellow-600 mb-4">
                            <i class="fas fa-exclamation-triangle text-3xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3">错误传播机制</h3>
                        <p class="text-gray-700">需要特殊机制将异常从工作线程传回主线程</p>
                    </div>
                    <div class="bg-green-50 p-6 rounded-lg hover-scale">
                        <div class="text-green-600 mb-4">
                            <i class="fas fa-shield-alt text-3xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3">资源管理</h3>
                        <p class="text-gray-700">异常可能导致资源泄漏，需要妥善处理</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Future Section -->
        <section id="future" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <span class="bg-indigo-100 text-indigo-700 w-10 h-10 rounded-full flex items-center justify-center mr-4">1</span>
                使用 Future 和 ExecutorService
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="method-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-indigo-100 text-indigo-700 p-2 rounded-lg mr-4">
                            <i class="fas fa-clock text-xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold">基本处理流程</h3>
                    </div>
                    <p class="text-gray-700 mb-4">当使用 <code>ExecutorService</code> 提交异步任务时，你可以使用 <code>Future</code> 来获取任务的结果。<code>Future.get()</code> 方法会在任务完成时返回结果，如果任务执行过程中抛出异常，<code>get()</code> 方法会抛出 <code>ExecutionException</code>。</p>
                    <div class="text-sm text-gray-500 flex items-center">
                        <i class="fas fa-info-circle mr-2"></i>
                        通过 <code>getCause()</code> 方法可以获取实际的异常
                    </div>
                </div>
                
                <div class="code-block rounded-lg overflow-hidden">
                    <div class="code-header">
                        <div class="text-gray-300 text-sm">Java</div>
                        <div class="text-gray-400 text-sm">
                            <i class="far fa-copy cursor-pointer hover:text-white" onclick="copyCode(this)"></i>
                        </div>
                    </div>
                    <div class="code-body">
                        <pre><code class="language-java">import java.util.concurrent.*;

public class FutureExceptionHandling {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        Future&lt;Integer&gt; future = executor.submit(() -&gt; {
            throw new RuntimeException("Something went wrong");
        });

        try {
            // 获取结果
            future.get();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt(); // 恢复中断状态
        } catch (ExecutionException e) {
            // 处理异步任务中的异常
            System.err.println("Exception occurred: " + e.getCause().getMessage());
        } finally {
            executor.shutdown();
        }
    }
}</code></pre>
                    </div>
                </div>
            </div>
            
            <div class="mt-8 bg-blue-50 border-l-4 border-blue-500 p-4 rounded-r-lg">
                <div class="flex">
                    <div class="flex-shrink-0 text-blue-500">
                        <i class="fas fa-lightbulb text-xl"></i>
                    </div>
                    <div class="ml-3">
                        <h3 class="text-sm font-medium text-blue-800">最佳实践</h3>
                        <div class="mt-2 text-sm text-blue-700">
                            <p>• 总是使用<code>finally</code>块关闭<code>ExecutorService</code></p>
                            <p>• 处理<code>InterruptedException</code>时恢复中断状态</p>
                            <p>• 对于长时间运行的任务，考虑使用带超时的<code>get()</code>方法</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- CompletableFuture Section -->
        <section id="completable-future" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <span class="bg-indigo-100 text-indigo-700 w-10 h-10 rounded-full flex items-center justify-center mr-4">2</span>
                使用 CompletableFuture
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="method-card bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-indigo-100 text-indigo-700 p-2 rounded-lg mr-4">
                            <i class="fas fa-project-diagram text-xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold">链式异常处理</h3>
                    </div>
                    <p class="text-gray-700 mb-4"><code>CompletableFuture</code> 提供了更灵活的异步编程模型。它支持链式调用和异常处理，可以通过 <code>handle()</code>、<code>exceptionally()</code> 和 <code>whenComplete()</code> 方法来处理异常。</p>
                    
                    <div class="mt-4">
                        <div class="flex items-start mb-2">
                            <div class="flex-shrink-0 text-indigo-500 mt-1">
                                <i class="fas fa-check-circle"></i>
                            </div>
                            <div class="ml-2 text-sm text-gray-700">
                                <code>handle()</code>: 无论成功失败都会调用，可访问结果和异常
                            </div>
                        </div>
                        <div class="flex items-start mb-2">
                            <div class="flex-shrink-0 text-indigo-500 mt-1">
                                <i class="fas fa-check-circle"></i>
                            </div>
                            <div class="ml-2 text-sm text-gray-700">
                                <code>exceptionally()</code>: 仅在异常时调用，可提供恢复值
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0 text-indigo-500 mt-1">
                                <i class="fas fa-check-circle"></i>
                            </div>
                            <div class="ml-2 text-sm text-gray-700">
                                <code>whenComplete()</code>: 类似handle但不会转换结果
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="code-block rounded-lg overflow-hidden">
                    <div class="code-header">
                        <div class="text-gray-300 text-sm">Java</div>
                        <div class="text-gray-400 text-sm">
                            <i class="far fa-copy cursor-pointer hover:text-white" onclick="copyCode(this)"></i>
                        </div>
                    </div>
                    <div class="code-body">
                        <pre><code class="language-java">import java.util.concurrent.CompletableFuture;

public class CompletableFutureExceptionHandling {
    public static void main(String[] args) {
        CompletableFuture&lt;Integer&gt; future = CompletableFuture.supplyAsync(() -&gt; {
            throw new RuntimeException("Something went wrong");
        });

        // 使用handle处理异常
        future.handle((result, ex) -&gt; {
            if (ex != null) {
                System.err.println("Exception occurred: " + ex.getMessage());
                return null; // 处理异常后返回一个默认值
            }
            return result;
        });

        // 或者使用exceptionally
        future.exceptionally(ex -&gt; {
            System.err.println("Exception occurred: " + ex.getMessage());
            return null; // 返回一个默认值或进行其他处理
        });

        // 阻塞等待以确保异步处理完成
        future.join();
    }
}</code></pre>
                    </div>
                </div>
            </div>
            
            <div class="mt-6 grid md:grid-cols-2 gap-6">
                <div class="bg-white p-6 rounded-lg shadow-sm border-t-4 border-purple-500">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">异常传播场景</h3>
                    <div class="mermaid">
                        graph LR
                            A[异步任务] -->|抛出异常| B[CompletableFuture]
                            B --> C{处理方法}
                            C -->|exceptionally| D[提供默认值]
                            C -->|handle| E[转换结果]
                            C -->|whenComplete| F[执行副作用]
                    </div>
                </div>
                <div class="bg-white p-6 rounded-lg shadow-sm border-t-4 border-green-500">
                    <h3 class="text-xl font-semibold mb-4 text-green-700">方法对比</h3>
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">方法</th>
                                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">是否接收异常</th>
                                    <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">是否转换结果</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                <tr>
                                    <td class="px-4 py-3 text-sm text-gray-700">exceptionally</td>
                                    <td class="px-4 py-3 text-sm text-gray-700">是</td>
                                    <td class="px-4 py-3 text-sm text-gray-700">是</td>
                                </tr>
                                <tr>
                                    <td class="px-4 py-3 text-sm text-gray-700">handle</td>
                                    <td class="px-4 py-3 text-sm text-gray-700">是</td>
                                    <td class="px-4 py-3 text-sm text-gray-700">是</td>
                                </tr>
                                <tr>
                                    <td class="px-4 py-3 text-sm text-gray-700">whenComplete</td>
                                    <td class="px-4 py-3 text-sm text-gray-700">是</td>
                                    <td class="px-4 py-3 text-sm text-gray-700">否</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </section>

        <!-- RxJava Section -->
        <section id="rxjava" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <span class="bg-indigo-100 text-indigo-700 w-10 h-10 rounded-full flex items-center justify-center mr-4">3</span>
                使用 RxJava 或 Project Reactor
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div>
                    <div class="method-card bg-white p-6 rounded-lg shadow-sm mb-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 text-indigo-700 p-2 rounded-lg mr-4">
                                <i class="fas fa-bolt text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold">RxJava 异常处理</h3>
                        </div>
                        <p class="text-gray-700 mb-4">对于响应式编程，RxJava 提供了丰富的异常处理机制。你可以使用 <code>onError</code> 操作符来处理流中的异常。</p>
                        
                        <div class="code-block rounded-lg overflow-hidden">
                            <div class="code-header">
                                <div class="text-gray-300 text-sm">Java</div>
                                <div class="text-gray-400 text-sm">
                                    <i class="far fa-copy cursor-pointer hover:text-white" onclick="copyCode(this)"></i>
                                </div>
                            </div>
                            <div class="code-body">
                                <pre><code class="language-java">import io.reactivex.rxjava3.core.Single;

public class RxJavaExceptionHandling {
    public static void main(String[] args) {
        Single&lt;Integer&gt; single = Single.fromCallable(() -&gt; {
            throw new RuntimeException("Something went wrong");
        });

        single.subscribe(
            result -&gt; System.out.println("Result: " + result),
            error -&gt; System.err.println("Exception occurred: " + error.getMessage())
        );
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div>
                    <div class="method-card bg-white p-6 rounded-lg shadow-sm">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 text-indigo-700 p-2 rounded-lg mr-4">
                                <i class="fas fa-atom text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold">Project Reactor 异常处理</h3>
                        </div>
                        <p class="text-gray-700 mb-4">Project Reactor 提供了类似的异常处理机制，通过订阅时的错误处理回调来捕获异常。</p>
                        
                        <div class="code-block rounded-lg overflow-hidden">
                            <div class="code-header">
                                <div class="text-gray-300 text-sm">Java</div>
                                <div class="text-gray-400 text-sm">
                                    <i class="far fa-copy cursor-pointer hover:text-white" onclick="copyCode(this)"></i>
                                </div>
                            </div>
                            <div class="code-body">
                                <pre><code class="language-java">import reactor.core.publisher.Mono;

public class ReactorExceptionHandling {
    public static void main(String[] args) {
        Mono&lt;Integer&gt; mono = Mono.fromCallable(() -&gt; {
            throw new RuntimeException("Something went wrong");
        });

        mono.subscribe(
            result -&gt; System.out.println("Result: " + result),
            error -&gt; System.err.println("Exception occurred: " + error.getMessage())
        );
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="bg-indigo-50 p-6 rounded-lg">
                <h3 class="text-xl font-semibold mb-4 text-indigo-800">响应式编程异常处理模式</h3>
                <div class="grid md:grid-cols-3 gap-4">
                    <div class="bg-white p-4 rounded-lg shadow-xs">
                        <div class="text-indigo-600 mb-2">
                            <i class="fas fa-undo-alt"></i>
                        </div>
                        <h4 class="font-medium mb-2">错误恢复</h4>
                        <p class="text-sm text-gray-700">使用<code>onErrorReturn</code>, <code>onErrorResume</code>等方法从错误中恢复</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow-xs">
                        <div class="text-indigo-600 mb-2">
                            <i class="fas fa-retweet"></i>
                        </div>
                        <h4 class="font-medium mb-2">重试机制</h4>
                        <p class="text-sm text-gray-700">使用<code>retry</code>, <code>retryWhen</code>实现自动重试逻辑</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow-xs">
                        <div class="text-indigo-600 mb-2">
                            <i class="fas fa-filter"></i>
                        </div>
                        <h4 class="font-medium mb-2">全局处理</h4>
                        <p class="text-sm text-gray-700">通过<code>doOnError</code>添加副作用或全局错误处理</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="mb-20">
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <h2 class="text-3xl font-bold mb-6 text-gray-800">异步异常处理总结</h2>
                    <div class="mermaid">
                        flowchart TB
                            A[选择异步处理方式] --> B[Future + ExecutorService]
                            A --> C[CompletableFuture]
                            A --> D[响应式框架]
                            B --> E[捕获ExecutionException]
                            C --> F[使用exceptionally/handle]
                            D --> G[实现onError回调]
                            E --> H[日志记录/恢复]
                            F --> H
                            G --> H
                            H --> I[健壮的异步应用]
                    </div>
                    
                    <div class="mt-8 grid md:grid-cols-2 gap-6">
                        <div class="bg-gray-50 p-6 rounded-lg">
                            <h3 class="text-xl font-semibold mb-4 text-gray-800">通用原则</h3>
                            <ul class="space-y-3">
                                <li class="flex items-start">
                                    <div class="flex-shrink-0 h-5 w-5 text-indigo-500">
                                        <i class="fas fa-check-circle"></i>
                                    </div>
                                    <p class="ml-2 text-gray-700">永远不要吞掉异常，至少记录日志</p>
                                </li>
                                <li class="flex items-start">
                                    <div class="flex-shrink-0 h-5 w-5 text-indigo-500">
                                        <i class="fas fa-check-circle"></i>
                                    </div>
                                    <p class="ml-2 text-gray-700">为不同类型的异常设计恢复策略</p>
                                </li>
                                <li class="flex items-start">
                                    <div class="flex-shrink-0 h-5 w-5 text-indigo-500">
                                        <i class="fas fa-check-circle"></i>
                                    </div>
                                    <p class="ml-2 text-gray-700">考虑使用断路器模式防止级联故障</p>
                                </li>
                            </ul>
                        </div>
                        <div class="bg-gray-50 p-6 rounded-lg">
                            <h3 class="text-xl font-semibold mb-4 text-gray-800">框架选择建议</h3>
                            <ul class="space-y-3">
                                <li class="flex items-start">
                                    <div class="flex-shrink-0 h-5 w-5 text-indigo-500">
                                        <i class="fas fa-check-circle"></i>
                                    </div>
                                    <p class="ml-2 text-gray-700">简单任务: <code>CompletableFuture</code></p>
                                </li>
                                <li class="flex items-start">
                                    <div class="flex-shrink-0 h-5 w-5 text-indigo-500">
                                        <i class="fas fa-check-circle"></i>
                                    </div>
                                    <p class="ml-2 text-gray-700">复杂异步流: <code>RxJava</code>或<code>Project Reactor</code></p>
                                </li>
                                <li class="flex items-start">
                                    <div class="flex-shrink-0 h-5 w-5 text-indigo-500">
                                        <i class="fas fa-check-circle"></i>
                                    </div>
                                    <p class="ml-2 text-gray-700">分布式系统: 考虑<code>Akka</code>等框架</p>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-12">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-bold">技术小馆</h3>
                    <p class="text-gray-400 mt-2">探索编程的无限可能</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-gray-500 text-sm">
                &copy; 2023 技术小馆. All rights reserved.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        function copyCode(button) {
            const codeBlock = button.closest('.code-block').querySelector('code');
            const range = document.createRange();
            range.selectNode(codeBlock);
            window.getSelection().removeAllRanges();
            window.getSelection().addRange(range);
            document.execCommand('copy');
            window.getSelection().removeAllRanges();
            
            const originalIcon = button.classList.contains('fa-copy') ? 'fa-copy' : 'fa-clipboard';
            button.classList.remove(originalIcon);
            button.classList.add('fa-check');
            
            setTimeout(() => {
                button.classList.remove('fa-check');
                button.classList.add(originalIcon);
            }, 2000);
        }
    </script>
</body>
</html>
```